<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Know extends CI_Controller {

	function __construct(){
		parent::__construct();
		authRedirect('/admin/index');
		$this->load->model('know_model');
	}
	public function index()
	{
		$data['error'] = $this->session->flashdata('error');
		$data['success'] = $this->session->flashdata('success');
		
		$start = $this->uri->segment(4,0);
		$data['know'] = $this->know_model->get_know($start,PAGENUM);
		$data['num'] = $this->know_model->get_know_num();

		//pagenation
		$this->load->library('pagination');
		$config['base_url'] = '/admin/know/index/';
		$config['total_rows'] = $data['num'];
		$config['per_page'] = PAGENUM; 
		$config['full_tag_open'] = '<p class="page">';
		$config['full_tag_close'] = '</p>';
		$config['uri_segment'] = 4;
		$config['first_link'] = '首页';
		$config['last_link'] = '尾页';
		$config['cur_tag_open'] = '<b>';
		$config['cur_tag_close'] = '</b>';
		$config['next_link'] = '下一页';
		$config['prev_link'] = '上一页';
		$this->pagination->initialize($config); 
		$data['page'] = $this->pagination->create_links();

		$this->load->view('admin/know/list.tpl',$data);
	}

	public function action(){
		
		$data['error'] = $this->session->flashdata('error');
		$data['success'] = $this->session->flashdata('success');

		$this->load->library('form_validation');
		$this->form_validation->set_rules('title', '标题', 'trim|required|xss_clean');
		$this->form_validation->set_rules('content', '内容', 'trim|required');
		$this->form_validation->set_rules('status', '邮箱', 'trim|required|xss_clean');
		if ($this->form_validation->run()) {
			$r = $this->know_model->action();
			if($r['status']){
				$this->session->set_flashdata('success','添加成功！');
				redirect('/admin/know');
			}else{
				$this->session->set_flashdata('error',$r['msg']);
				redirect('/admin/know/action');
			}
		}
		
		$data['know'] = '';
		$data['action'] = '/admin/know/action';
		$data['button'] = '添加动态';
		if((int)$this->uri->segment(4)){
			$data['know'] = $this->know_model->get_know_byid((int)$this->uri->segment(4));
			$data['action'] = '/admin/know/update/' . (int)$this->uri->segment(4);
			$data['button'] = '更新商品';
		}
		$this->load->view('admin/know/action.tpl',$data);
	}

	public function update(){
		$id = (int)$this->uri->segment(4);
		if(!$id) show_error('错误的操作');

		$p = $this->know_model->get_know_byid($id);
		if(!$p) show_error('该专业知识可能已经被删除');

		$this->load->library('form_validation');
		$this->form_validation->set_rules('title', '标题', 'trim|required|xss_clean');
		$this->form_validation->set_rules('content', '内容', 'trim|required');
		$this->form_validation->set_rules('status', '邮箱', 'trim|required|xss_clean');
		if($this->form_validation->run()){
			$r = $this->know_model->update($id);
			if($r['status']){
				$this->session->set_flashdata('success','更新成功！');
				redirect('/admin/know');
			}else{
				$this->session->set_flashdata('error',$r['msg']);
				redirect('/admin/know/action/' . $id);
			}
		}else{
			$this->session->set_flashdata('error',validation_errors());
			redirect('/admin/know/action/' . $id);
		}
	}
}

/* End of file production.php */
/* Location: ./application/controllers/admin/production.php */